﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class KfDAL
    {
        public static int Save(KfInfo info)
        {
            string sql = string.Format("insert into [kf]([Mobile],[FName]) values('{0}','{1}');select @@identity;", info.Mobile, info.FName);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static int Update(KfInfo info)
        {
            string sql = string.Format("update [kf] set [Mobile]='{0}',[FName]='{1}'  where ID={2}", info.Mobile, info.FName, info.ID);
            return CommonDAL.ExecuteNoneQuery(sql) ? info.ID : 0;
        }
        public static KfInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[kf]");
            return Simple_Data(dr);
        }
        public static List<KfInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[kf]");
            List<KfInfo> list = new List<KfInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<KfInfo> FindPage(string keyword, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = "";
            if (!string.IsNullOrEmpty(keyword))
            {
                filter += " and ([kf].FName like '%" + keyword + "%' or [kf].Mobile like '%" + keyword + "%')";
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[kf]", out count);
            List<KfInfo> list = new List<KfInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<KfInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [kf]"));
            List<KfInfo> list = new List<KfInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        
        private static KfInfo Simple_Data(DataRow dr)
        {
            KfInfo info = new KfInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.FName = DataConvert.ToString(dr["FName"]);
            }
            return info;
        }
        
    }
}
